package com.kws.merchant.mapper;

import com.kws.merchant.domain.ContractSignature;
import com.kws.merchant.domain.DeviceRemark;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kws.merchant.domain.vo.UserRemarkVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * 设备评论表(DeviceRemark)表数据库访问层
 *
 * @author yxy
 * @since 2024-10-22 15:26:02
 */

public interface DeviceRemarkMapper extends BaseMapper<DeviceRemark> {

    @Select("<script>" +
            "SELECT " +
            " count(a.device_id) " +
            "FROM " +
            " tb_device_remark AS a " +
            " LEFT JOIN sys_user AS b ON a.observer = b.user_id  " +
            "WHERE " +
            " a.pid = 0 " +
            " AND a.enable = 1 " +
            " AND a.device_id = ${goodsId} " +
            " AND a.remark_type = '${remarkType}' " +
            " <if test='tag!=null '>  AND a.tag = #{tag}  </if>  " +
            "</script>")
    Integer getGoodsRemarkCount(@Param("goodsId") Long goodsId,
                                @Param("tag") Integer tag,
                                @Param("remarkType") String remarkType);

    @Select("<script>" +
            "SELECT " +
            " a.device_id as goods_id, " +
            " a.create_time, " +
            " a.star_level, " +
            " a.remark, " +
            " a.remark_img, " +
            " a.remark_type as goods_type, " +
            " b.user_name as observer, " +
            " b.avatar as observer_portrait " +
            "FROM " +
            " tb_device_remark AS a " +
            " LEFT JOIN sys_user AS b ON a.observer = b.user_id  " +
            "WHERE " +
            " a.pid = 0 " +
            " AND a.enable = '1' " +
            " AND a.device_id = ${goodsId} " +
            " AND a.remark_type = '${remarkType}' " +
            " <if test='tag!=null '>  AND a.tag = #{tag}  </if>  " +
            " limit #{page},#{size}" +
            "</script>")
    List<UserRemarkVo> getGoodsRemarkList(@Param("goodsId") Long goodsId,
                                          @Param("tag") Integer tag,
                                          @Param("remarkType") String remarkType,
                                          @Param("page") Integer page,
                                          @Param("size") Integer size);

    @Select("SELECT " +
            " a.device_id as goods_id, " +
            " a.create_time, " +
            " a.star_level, " +
            " a.remark_img, " +
            " a.remark_type as goods_type, " +
            " a.observer, " +
            " b.avatar as observer_portrait " +
            " FROM " +
            " tb_device_remark AS a " +
            " LEFT JOIN sys_user AS b ON a.observer = b.user_id  " +
            "WHERE " +
            " a.enable = '1' " +
            " AND a.observer = ${userId} " +
            " limit #{page},#{size} ")
    List<UserRemarkVo> getUserRemarkList(@Param("userId") Long userId,
                                         @Param("page") Integer page,
                                         @Param("size") Integer size);

    @Select("SELECT count(*) from tb_device_remark s LEFT JOIN sys_user u on s.observer=u.user_id where ${sql}")
    int countToTal(String sql);

    @Select("SELECT  s.*,u.user_name as observerName from tb_device_remark s LEFT JOIN sys_user u on s.observer=u.user_id where ${sql} limit ${sizeNum},${totalPage}")
    List<DeviceRemark> getRecords(@Param("sql") String sql, @Param("sizeNum") Integer sizeNum, @Param("totalPage") Integer totalPage);
}
